<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>

<body>
  <script>
    let arr = [2, 5, 4, 3, 8, 7, 1, 6, 9]
    let arr1 = [2, 5, 4, 3, 8, 7, 1, 6, 9]
    for (let i = 0; i < arr.length - 1; i++) {
      for (let j = 0; j < arr.length - i - 1; j++) {
        //开始交换，如果第一个数大于第二个数才交换
        if (arr[j] > arr[j + 1]) {
          //交换两个变量
          let temp = arr[j]
          arr[j] = arr[j + 1]
          arr[j + 1] = temp
        }
      }
    }
    console.log(arr);
    //上面的原理相当于这个函数sort
    //升序(默认)
    arr1.sort()
    console.log(arr1);
    //降序
    arr1.sort(function (a, b) {
      return b - a  //反之升序就是 a - b
    })
    console.log(arr1);

  </script>
</body>

</html>